//package com.example.chat.service.impl;
//
//import com.example.chat.service.EmailService;
//import jakarta.mail.internet.MimeMessage;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSender;
//import org.springframework.mail.javamail.MimeMessageHelper;
//import org.springframework.stereotype.Service;
//
//@Service
//public class EmailServiceImpl implements EmailService {
//
//    @Autowired
//    private JavaMailSender mailSender;
//
//    @Override
//    public void sendEmail(String to, String subject, String content) {
//        try {
//            MimeMessage message = mailSender.createMimeMessage();
//            MimeMessageHelper helper = new MimeMessageHelper(message, true); // true 表示 multipart (多部分)
//
//            helper.setTo(to);
//            helper.setSubject(subject);
//            helper.setText(content, true); // 第二个参数 true 表示这是 HTML 格式
//            helper.setFrom("2350776830@qq.com");
//
//            mailSender.send(message);
//            System.out.println("邮件发送成功: " + to);
//        } catch (Exception e) {
//            System.err.println("邮件发送失败: " + e.getMessage());
//            e.printStackTrace();
//        }
//    }
//}
package com.example.chat.service.impl;

import com.example.chat.service.EmailService;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendEmail(String to, String subject, String content) {
        try {
            // 创建一个MimeMessage对象
            MimeMessage message = mailSender.createMimeMessage();

            // 使用MimeMessageHelper来创建邮件
            MimeMessageHelper helper = new MimeMessageHelper(message, true); // true 表示 multipart (多部分)

            // 设置发件人
            helper.setFrom("18207472736@163.com");
            // 设置收件人
            helper.setTo(to);
            // 设置邮件主题
            helper.setSubject(subject);
            // 设置邮件内容，第二个参数为 true 表示内容是 HTML 格式
            helper.setText(content, true);

            // 发送邮件
            mailSender.send(message);
            System.out.println("邮件发送成功: " + to);
        } catch (Exception e) {
            System.err.println("邮件发送失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
